﻿using Prism.Mvvm;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Media.Media3D;
using DTheme;
using System.Windows.Data;
using System.ComponentModel;
using System.Data;
using System.Windows.Media;
using System.Threading;

using LxCFD;

namespace TimeSolve
{
    public class MainViewModel : BindableBase, IDataUpdate
    {
        public static MainViewModel mainViewModel { get; set; }

        public MainViewModel(MainWindow mainWindow)
        {
            MainViewModel.mainViewModel = this;//给全局变量赋值
            this.mainWindow = mainWindow;
            log = new LogWin(mainWindow.vconsole);
            unitManager = new UnitManager();
            ICollectionView cv = CollectionViewSource.GetDefaultView(graphs);
            cv.GroupDescriptions.Add(new PropertyGroupDescription("graphType"));

            #region delegatecommand初始化区域
            test = new DelegateCommand(_test);
            readFile = new DelegateCommand(_readFile);
            changeTasksNum = new DelegateCommand(_changeTasksNum);
            writeFile = new DelegateCommand(_writeFile);
            creatSolverSubWin = new DelegateCommand<object>(_creatSolverSubWin);
            creatConditionSubwin = new DelegateCommand<object>(_creatConditionSubwin);
            creatGraphSubwin = new DelegateCommand<object>(_creatGraphSubwin);
            #endregion


            log.AddWithTime("欢迎使用");

            #region 临时

            #endregion
        }
        public LogWin log { get; set; }
        public MainWindow mainWindow { get; set; }
        public bool isBusy { get => _isBusy; set { _isBusy = value; RaisePropertyChanged("isBusy"); } }
        private bool _isBusy;
        public bool isCalculating { get => ComputeState.GetIsRunning(); }

        public int tasksNum
        {
            get => MultiThreads.GetThreadsCount();
            set
            {
                MultiThreads.SetThreadsCount(value);
                RaisePropertyChanged("tasksNum");
            }
        }
        public DaoHangPanels? daohangPanel { get => _daohangPanel; set { _daohangPanel = value; RaisePropertyChanged("daohangPanel"); } }
        private DaoHangPanels? _daohangPanel = DaoHangPanels.帮助;
        public UnitManager unitManager { get; set; }

        public List<IDataUpdate> updateAndGetChildren()
        {
            return null;
        }
        #region mainWindow中的数据

        public MeshVM mesh { get => _mesh; set { _mesh = value; RaisePropertyChanged("mesh"); } }
        private MeshVM _mesh;
        public ObservableCollection<ContinuumVM> continuums { get; set; } = new ObservableCollection<ContinuumVM>();
        //public ObservableCollection<MonitorItem> monitors { get; set; } = new ObservableCollection<MonitorItem>();
        public GraphDataBase graphDataBase { get; set; }
        public ObservableCollection<GraphItem> graphs { get; set; } = new ObservableCollection<GraphItem>();

        public object selectedObject
        {
            get => _selectedObject;
            set
            {
                _selectedObject = value;
                RaisePropertyChanged("selectedObject");
            }
        }
        private object _selectedObject;

        #endregion

        #region command区域
        public DelegateCommand test { get; set; }
        public void _test()
        {
            //TaskScheduler uitask = TaskScheduler.FromCurrentSynchronizationContext();
            //Task.Factory.StartNew(() =>
            //{
            //    mainViewModel.log.AddWithTime("-=-=-=-=");
            //}, new CancellationTokenSource().Token, TaskCreationOptions.None, uitask);
        }
        public DelegateCommand readFile { get; set; }
        private void _readFile()
        {
            ReadFileDialog r = new ReadFileDialog();
            r.readFile(ref _mesh);
            //if (mesh.nodes.Length == 0) return;//未完，应该有详细的判断
            //buildGraph.setPoint3Ds(mesh.nodes);
        }
        public DelegateCommand writeFile { get; set; }
        private void _writeFile()
        {
            WriteFileDialog w = new WriteFileDialog();
            w.WriteFile();
        }
        public DelegateCommand changeTasksNum { get; set; }
        private void _changeTasksNum()
        {
            ChangeTasksNumWindow c = new ChangeTasksNumWindow();
            c.ShowDialog();
        }
        public DelegateCommand<object> creatSolverSubWin { get; set; }
        private void _creatSolverSubWin(object button)
        {
            DPathButton bt = button as DPathButton;
            if (bt == null) return;
            creatSubWin<SolverWindow>(bt);
        }
        public DelegateCommand<object> creatConditionSubwin { get; set; }
        private void _creatConditionSubwin(object button)
        {
            DPathButton bt = button as DPathButton;
            if (bt == null) return;
            creatSubWin<ConditionWindow>(bt);
        }
        public DelegateCommand<object> creatGraphSubwin { get; set; }
        private void _creatGraphSubwin(object button)
        {
            DPathButton bt = button as DPathButton;
            if (bt == null) return;
            creatSubWin<GraphManageWindow>(bt);
        }
        public List<Window> subWindows { get; set; } = new List<Window>();
        private void creatSubWin<T>(DPathButton dPathButton) where T : Window, new()
        {
            // Console.WriteLine(111);
            foreach (Window win in subWindows)
            {
                //Console.WriteLine(222);
                //Console.WriteLine(typeof(T));
                //Console.WriteLine(win.GetType());
                if (typeof(T) == win.GetType())
                {
                    if (win.WindowState == System.Windows.WindowState.Minimized)
                    {
                        win.WindowState = System.Windows.WindowState.Normal;
                    }
                    win.Activate();
                    return;
                }
            }
            Window newwin = new T();
            subWindows.Add(newwin);
            newwin.Closed += (s, e) =>
            {
                for (int i = 0; i < subWindows.Count; i++)
                {
                    if (s.GetType() == subWindows[i].GetType())
                    {
                        subWindows.RemoveAt(i);
                        break;
                    }
                }
                dPathButton.IsActive = false;
            };
            dPathButton.IsActive = true;
            newwin.Show();
        }
        #endregion

        public void updateAllData()
        {
            UpdateData up = new UpdateData();
            up.start(this);
        }

    }

}
